<?php
class UserSelectController extends Zone_Action{
	public function initAction(){
		$configs = $this->Model->fetchRow("SELECT * FROM `configs` LIMIT 1");		
		$user_id   = get_user_id();
		$user = $this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$user_id'");	
			
		$this->set(array(
			configs	=> $configs,
			user	=> $user
		));
	}	
	
	public function multicheckAction(){
		$this->removeLayout();
		$selected = array();
		foreach( explode( get('str_user',' '),',') as $id ){
			if( (int)$id != 0 ){
				$selected[] = (int)$id;
			}
		}		
		$selected = implode(',',$selected);		
		$name 			= get('name');
		$is_checkbox 	= get('is_checkbox',1);
		$this->setJSON(array(
			content	=> $this->getTreeCheckbox(0, 0, $selected, $name, $is_checkbox)
		));	
	}	
			
	protected function getTreeCheckbox( $id=0, $depth = 0, $selected='', $name='users', $is_checkbox = 1){
		$sub_departments 
			= $this->Model->fetchAll("SELECT 
					`a`.*,IF(`b`.`ID`,`b`.`ID`,0) as `parentID` 
				FROM `departments` as `a` 
				LEFT JOIN `departments` as `b` 
					ON `a`.`parent_id`=`b`.`ID` 
				HAVING `parentID`='$id' ORDER BY `ord`");
		
		$staffs = $this->Model->fetchAll("SELECT *,
			IF( '$selected' LIKE CONCAT('%,',`ID`,'%') , ' checked', '' ) as `checked`
			FROM `users` 
			WHERE 
				`department_id`='$id' 
				AND `is_deleted`='no'");

		if(count($staffs) == 0 && count($sub_departments) == 0){
			return '';
		}
		
			
		//class
		$class = $depth == 0 ? "tree-list tree-root":"tree-list";
		$type = $is_checkbox == 1 ? 'checkbox' : 'radio'; 
			
		$list = "<div class='tree-list-outer'>
			<div class=\"$class\">";
		foreach( $sub_departments as $k => $sub ){
			$subs = $this->getTreeCheckbox( $sub['ID'], $depth+1, $selected, $name, $is_checkbox );
			
			$cl = $subs !="" ? "" : " tree-link-empty";
			$cls = $subs !="" ? "" : " tree-folder-empty";
			
			//show child in depth
			$has_sub  = $this->Model->fetchOne("SELECT `ID` FROM `departments` WHERE `parent_id`='{$sub['ID']}'");
			$is_close = $has_sub ? "" : " tree-section-close";
			
			$check_select = $is_checkbox == 1 
				? "<input type='checkbox' class='tree-check' onclick='task_check_all(this)'/>"
				: '';
			$list .="<div class='tree-folder-outer'>
				<a class='tree-node' onclick='tree_collapse(this)'></a>
				<div class='tree-folder'>
					<a class='tree-title'>				$check_select
						{$sub['title']}</a>
					$subs
				</div></div>";
		}	
		
		foreach( $staffs as $k => $staff ){
			$class = '';
					
			$list .="<a class='tree-item-outer'>
				<span class='tree-item' title='{$staff['username']}'><span class='tree-item-icon'></span>
				<input type='$type' name='{$name}' value='{$staff['ID']}'{$staff['checked']}' x-label='{$staff['username']}' x-title='{$staff['name']}' class='tree-check'/>
				<span class='chaticon-{$staff['ID']}'><span class='offline'></span></span>	
				<span class='$class'>{$staff['username']}</span></span>
				</a>";					
		}
		
		$list .="</div></div>";
		return $list;
	}
		
	public function suggestAction(){
		$this->removeLayout();
		$s = get('s','');
		if( $s == '' ){
			return $this->setJSON(array( content => array() ));
		}
		$this->setJSON(array(
			content	=> $this->Model->fetchAll("SELECT 
					`a`.`username`,
					`a`.`ID`,
					`b`.`title` as `department_title`
				FROM `users` as `a` 
				LEFT JOIN `departments` as `b`
					ON `a`.`department_id`=`b`.`ID`
				WHERE 
					`a`.`is_deleted`='no' 
					AND `a`.`username` LIKE '$s%' LIMIT 10")
		));	
	}	
}	